package cn.wolfcode.service.impl;

import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryItemServiceImpl implements ISystemDictionaryItemService {

    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;

    @Override
    public void save(SystemDictionaryItem systemDictionaryItem) {
        systemDictionaryItemMapper.insert(systemDictionaryItem);
    }

    @Override
    public void delete(Long id) {
        systemDictionaryItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(SystemDictionaryItem systemDictionaryItem) {
        systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
    }

    @Override
    public SystemDictionaryItem get(Long id) {

        SystemDictionaryItem systemDictionaryItem = systemDictionaryItemMapper.selectByPrimaryKey(id);
        return systemDictionaryItem;
    }

    @Override
    public List<SystemDictionaryItem> listAll() {

        List<SystemDictionaryItem> list = systemDictionaryItemMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<SystemDictionaryItem> query(QueryObject qo) {
        /*int count = systemDictionaryItemMapper.selectForCount(qo);
        if (count == 0) {
            PageResult<SystemDictionaryItem> pageResult = new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), count, Collections.emptyList());
            return pageResult;
        }

        //获取总页数
        int totalPage = count % qo.getPageSize()== 0
                ? count /qo.getPageSize() : count/ qo.getPageSize()+1;
        //判断当前页是否大于总页数,如果是,把总页数作为当前页查询分页结果集
        if (qo.getCurrentPage()>totalPage){
            qo.setCurrentPage(totalPage);
        }*/

        //使用分页插件,传入当前页,每页显示数量
        //会对下一句进行分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemMapper.selectForList(qo);
        return new PageInfo<>(systemDictionaryItems);
    }

    @Override
    public List<SystemDictionaryItem> selectByParentSn(String sn) {

        return systemDictionaryItemMapper.selectByParentSn(sn);
    }

}
